package com.cff.common;

import java.io.UnsupportedEncodingException;


/**
 * 瀛楃澶勭悊宸ュ叿
 * 
 * @author 鏉庢绔�
 *
 * 鍒涘缓鏃ユ湡锛�2012-4-10
 * @version 1.0
 * @since 1.0
 */
public class StringUtil {
	private static final String CHARSET_NAME = "UTF-8";

	public static boolean isEmpty(String str) {
		if(str == null || "".equals(str)) return true;
		return false;
	}

	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}

	public static String rtrim(String value) {
		if (value == null) {
			return null;
		}
		return value.replaceAll("\\s+$", "");
	}

	public static String ltrim(String value) {
		if (value == null) {
			return null;
		}
		return value.replaceAll("^\\s+", "");
	}

	/**
	 * 灏嗕竴涓瓧绗︿覆鎸夌潃鈥渦tf-8鈥濇牸寮忚浆鎹㈡垚瀛楄妭杩涜鎷艰
	 * @param str
	 * @return
	 */
	public static String stringToBytes(String str) {
		byte[] b = null;
		try {
			b = str.getBytes("utf-8");
		} catch (UnsupportedEncodingException e) {
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < b.length; i++) {
			sb.append(b[i]).append("/");
		}
		return sb.toString();
	}
	
	/**
	 * 
	 * 灏嗗瓧绗︿覆闀垮害瓒呰繃len鐨勫瓧绗︿覆绠�鍐欎负缂╁啓鏍煎紡锛屼緥濡傦細aaddssss ---> aadds鈥︹��
	 * @param str 闇�瑕佺缉鍐欑殑瀛楃涓�
	 * @param len 瀛楃涓茬缉鍐欓暱搴﹁姹�
	 * @return
	 */
	public static String stringForShort(String str, int len) {
		if (StringUtil.isNotEmpty(str) && str.length() >= len) {
			return str.substring(0, len-1) + "鈥︹��";
		}
		return str;
	}
}
